<template>
  <div id="userRegister">
    <h2 class="title">智能云图库 - 注册</h2>
    <a-form
      :model="loginForm"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
      :labelCol="{ span: 6 }"
    >
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入用户名!' }]">
        <a-input v-model:value="loginForm.userAccount" placeholder="请输入用户名" />
      </a-form-item>

      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码!' },
          { min: 8, message: '密码长度不能小于8位' },
        ]"
      >
        <a-input-password v-model:value="loginForm.userPassword" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item
        name="checkPassword"
        :rules="[
          { required: true, message: '请确认密码!' },
          { min: 8, message: '密码长度不能小于8位' },
        ]"
      >
        <a-input-password v-model:value="loginForm.checkPassword" placeholder="请确认密码" />
      </a-form-item>
      <div class="tips">已有账号？<RouterLink to="/user/login">去注册</RouterLink></div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { userRegisterUsingPost } from '@/api/userController'
import { useUserLoginUserStore } from '@/stores/loginUserStore'
import { message } from 'ant-design-vue'
import { reactive } from 'vue'
import { useRouter } from 'vue-router'

// 表单值
const loginForm = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})
const router = useRouter()
const loginUserStore = useUserLoginUserStore()
const handleSubmit = async (values: API.UserLoginRequest) => {
  const res = await userRegisterUsingPost(values)
  if (res.data.code === 0) {
    message.success('注册成功')
    router.replace({
      path: '/user/login',
    })
  }
}
</script>

<style scoped>
#userRegister {
  max-width: 22.5rem;
  margin: 0 auto;
}
.title {
  text-align: center;
}
.tips {
  text-align: right;
  margin-bottom: 1rem;
  color: #bbb;
}
</style>
